package leetcode_core.sort;

import org.junit.Test;
import org.junit.experimental.max.MaxHistory;

import java.util.Arrays;

public class SelectionSort {
    public void selectionSort(int[] arr){
        //1.处理边界条件
        if(null == arr || arr.length <2){//arr.length ==0 || arr.length == 1
            return;
        }
        //处理从i-N-1
        for(int i = 0 ;i<arr.length;i++){
            int minIdx = i;
            for(int j = i+1;j<arr.length;j++){
                //处理从i-N-1间的最小值
                minIdx = arr[j] < arr[minIdx] ? j:minIdx;
            }
            swap(arr,i,minIdx);
        }
    }

    private void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    @Test
    public void test(){
        int[] arr = {7, 1, 3, 5, 1, 6, 8, 1, 3, 5, 7, 5, 6};
        System.out.println(Arrays.toString(arr));
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
